import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;


public class cont1 extends JFrame implements ActionListener
{
   // a JTextField for each number and one for the result
   private final JTextField number1JTextField = new JTextField(25);
   //  private final JTextField number2JTextField = new JTextField(10);
   //private final JTextField number3JTextField = new JTextField(20);
   private final JTextField resultJTextField = new JTextField(25);
   JLabel loginLabel = new JLabel("");
   JButton computeJButton = new JButton("Login");
  
   // Construct a new GCD window with 3 textfields so that we calculte 
   public cont1()
   {  
      Container contents = getContentPane();
      // set the title to GCD
      setTitle("Holiday_request");

      // set the layout to 1 per column
      contents.setLayout(new GridLayout(0, 1));

      // Add  textfields on the contents
      contents.add(new JLabel("driver ID: "));
      contents.add(number1JTextField);
     
       
      // create a new button and add it to the contents
      contents.add(computeJButton);
      // add a ActionListener for this button
      computeJButton.addActionListener(this);
      // add in the contents a textlabel 
      contents.add(loginLabel);

     // contents.add(result1);
      // display the result in the restultextfield
    //  contents.add(resultJTextField);
      // exit the program in case the window it's closed
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
   } //cont
   
 
   private JLabel statusReq;
   private final JTextField startDate = new JTextField(25);
   private final JTextField endDate = new JTextField(25);
   JButton reqHoliday = new JButton("Request Holiday");
   //driverLogedIn
   int driverId;
   public cont1(int driverID)
   {
      Container contents = getContentPane();
      setTitle("Holiday_request");
      driverId=driverID;
      // set the layout to 1 per column
      contents.setLayout(new GridLayout(0, 2));
      // Add  textfields on the contents
      contents.add(new JLabel("Welcome " + DriverInfo.getName(driverID) + ". Holidays taken: " + DriverInfo.getHolidaysTaken(driverID)));
      contents.add(new JLabel(""));
      contents.add(new JLabel("Start Date of holiday: day/month/year"));contents.add(startDate);
      contents.add(new JLabel("End Date of holiday:   day/month/year"));contents.add(endDate);
      contents.add(reqHoliday);      reqHoliday.addActionListener(this);
      statusReq = new JLabel("status");
      contents.add(statusReq);

      // exit the program in case the window it's closed
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
   }//cont 
  // 
   public void actionPerformed(ActionEvent event)
   {    
      if (event.getSource() == computeJButton)
      { if(number1JTextField.equals(null)) return;   
        int number1 = Integer.parseInt(number1JTextField.getText());
        int holidaysNo= DriverInfo.getHolidaysTaken(number1);
        if(DriverInfo.getName(number1)!="" && holidaysNo<25)
        {
          log.setVisible(false);
          cont x = new cont(number1);
          x.setVisible(true);
        }
        else if(DriverInfo.getHolidaysTaken(number1)>=25)  loginLabel.setText("You already have 25 holidays free this year");
             else loginLabel.setText("ID invalid");
        
      }//if
      if(event.getSource()==reqHoliday)
           {    
                	
		String[] startWords=startDate.getText().split("/");
		String[] endWords = endDate.getText().split("/");
		if(startWords.length<=3&&endWords.length<=3)
                {
	 Date start = new GregorianCalendar(Integer.parseInt(startWords[2]), Integer.parseInt(startWords[1]), Integer.parseInt(startWords[0])).getTime();
     		 Date end = new GregorianCalendar(Integer.parseInt(endWords[2]), Integer.parseInt(endWords[1]), Integer.parseInt(endWords[0])).getTime();
 		if(checkReq(start,end))statusReq.setText("Holiday approved.You have currently "+ DriverInfo.getHolidaysTaken(driverId) + " holidays days");
                }

		
           }//if

   } // actionPerformed
   public boolean checkReq(Date start, Date end)
   {
      long diff = (end.getTime()-start.getTime())/(1000 * 60 * 60 * 24);
      if(diff<=0){statusReq.setText("The date difference are not good. Try again. -" + diff);return false;}
      if(diff+DriverInfo.getHolidaysTaken(driverId) > 25){statusReq.setText("Exceeded 25 days limit. Try again -" + diff);return false;}
      //check only 10 holidays/day
      int[] driverIDs = DriverInfo.getDrivers();
      for (int i=0; i<driverIDs.length; i++);
      
     // set all the days
      while (!start.equals(end))
      {
          DriverInfo.setAvailable(driverId, start, false) ;
          start.setDate(start.getDate()+1);
          System.out.println("Next day: "+ start.getDate());
      }//while
     // DriverInfo.setAvailable(driverId, dateIndex, true) ;
      DriverInfo.setHolidaysTaken(driverId, (int)diff+DriverInfo.getHolidaysTaken(driverId)); return true;
   }
   public static cont log;
   // create a new cont and display it
   public static void main(String[] args)
   {
      database.openBusDatabase();
      log = new cont();
      log.setVisible(true);
   } // main
} // class cont
